﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataEditor.DataLayer;
using System.Collections.ObjectModel;

namespace DataEditor.ViewModels
{
    public class SymbolsViewModel : BaseViewModel
    {

        #region constructonrs

        public SymbolsViewModel()
        {
            MakeCommands();
        }

        #endregion

        #region properties
        private ObservableCollection<SymbolItemViewModel> _symbolItems;

        public ObservableCollection<SymbolItemViewModel> SymbolItems
        {
            get
            {
                _symbolItems = new ObservableCollection<SymbolItemViewModel>();


                var query = from u in dataContext.UNIT_SYMBOL
                            select u;
                foreach (var item in query)
                {
                    _symbolItems.Add(new SymbolItemViewModel(item));
                }

                return _symbolItems;
            }
            set
            {
                _symbolItems = value;
                NotifyOfPropertyChange(() => SymbolItems);
            }
        }

        RelayCommand _addNewCommand;
        public RelayCommand AddNewCommand
        {
            get { return _addNewCommand; }
            set
            {
                _addNewCommand = value;
                NotifyOfPropertyChange(() => AddNewCommand);
            }
        }

        RelayCommand _removeCommand;
        public RelayCommand RemoveCommand
        {
            get { return _removeCommand; }
            set
            {
                _removeCommand = value;
                NotifyOfPropertyChange(() => RemoveCommand);
            }
        }

        private SymbolItemViewModel selectedSymbolItem;
        public SymbolItemViewModel SelectedSymbolItem
        {
            get { return selectedSymbolItem; }
            set 
            { 
                selectedSymbolItem = value;
                NotifyOfPropertyChange(() => SelectedSymbolItem);
            }
        }

        #endregion


        #region Action/methods
        private void MakeCommands()
        {
            _addNewCommand = new RelayCommand(
                param =>
                {
                    UNIT_SYMBOL us = new UNIT_SYMBOL()
                        {
                            SYMBOL = string.Empty
                        };
                    dataContext.UNIT_SYMBOL.AddObject(us);
                    dataContext.SaveChanges();
                    SymbolItems.Add(new SymbolItemViewModel(us));
                    NotifyOfPropertyChange(() => SymbolItems);
                }
            );

            _removeCommand = new RelayCommand(
                param =>
                {
                    dataContext.UNIT_SYMBOL.DeleteObject(SelectedSymbolItem.Model);
                    dataContext.SaveChanges();
                    SymbolItems.Remove(SelectedSymbolItem);
                    NotifyOfPropertyChange(() => SymbolItems);
                },
                    (obj) =>
                    {
                        return SelectedSymbolItem != null;
                    }
            );

        }

        public void Save()
        {
            dataContext.SaveChanges();
        }
        #endregion


    }
}
